package com.sisqorap.puzzelalarm;

import java.util.Random;

import android.util.Log;

public class Puzzel{
	private int list[];
	
	public Puzzel(int puzzlePieces){
		list = new int[puzzlePieces];
		Random randomGenerator = new Random();
		for(int i=0; i<list.length; i++){
			Log.v("i:" , i+"");
			list[i] = randomGenerator.nextInt(100);
			//Check if randomNumber isn't used already
			Log.v("Equals", list[i]+"");
			//if(list.equals(list[i])){
			//if(Arrays.asList(list).contains(list[i])){
			if(checkDuplicated_withNormal(list)){
				Log.v("Zelfde nummer gevonden", list[i]+"");
				i--;
			}
		}
	}
	
	//check duplicated value
	private boolean checkDuplicated_withNormal(int[] sValueTemp)
	{
		for (int i = 0; i < sValueTemp.length; i++) {
			int sValueToCheck = sValueTemp[i];
			if(sValueToCheck==0){// || sValueToCheck.equals(0)){
				continue; //empty ignore
			}
			for (int j = 0; j < sValueTemp.length; j++) {
				if(i==j){
					continue; //same line ignore
				}
				int sValueToCompare = sValueTemp[j];
				if (sValueToCheck==sValueToCompare){
					return true;
				}
			}
		}
		return false; 
	} //http://www.mkyong.com/java/check-duplicated-value-in-array/
	
	public int[] listPuzzelPieces()
	{
		return list;
	}
	
	public boolean SolveEasyPuzzel(int selectedNumber){
		int smallestInList = min(list);
		boolean result = false;
		int resultList[] = new int[list.length-1];
		if(selectedNumber == smallestInList){
			result = true;
			int resultI = 0;
			for(int i = 0; i < list.length; i++){
				if(list[i] != smallestInList){
					resultList[resultI] = list[i];
					resultI++;
				}
			}
		}
		if(result){
			list = resultList;
		}
		return result;
	}

	private int min(int[] a, int... reject) {
	    int res = Integer.MAX_VALUE;
	    for (int i = 0; i < a.length; i++) {
	        boolean skip = false;
	        for (int r : reject) {
	            if (i == r) {
	                skip = true;
	                break;
	            }
	        }
	        if (!skip) {
	            res = Math.min(a[i], res);
	        }
	    }
	    return res;
	}
	
	public boolean isPuzzelSolved(){
		boolean result = false;
		if(list.length == 0){
			result = true;
		}
		return result;
	}
}